package Chess.GUI;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import Chess.*;
import Chess.Moves.Move;
import Chess.Moves.Move.MoveType;
import Chess.Pieces.Piece;

/**
 *	A GUI JFrame and controller for the Chess game.
 *	Implements ActionListener.
 *
 *	@author		Jeff Chan, Bryant Vu
 */
public class GUIController extends JFrame implements ActionListener{
	private Game game;
	private String lastSelection = "";
	private ImagePanel panel;
	private JButton lastSelectionButton;
	
	public GUIController() {
		game = new Game(); // New Game
		
		createPanel();
		paintSquares();
		getContentPane().add(panel);
		createMenu();
		
		// Sets the settings and displays the window.
		setSize(new Dimension(806, 853)); //811,839
		setResizable(false);
		setTitle("Chess Fo Sho");
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Background audio thread
		new Audio("../src/GUI/DonkeyKong.wav").start();
	}
	
	public void createPanel() {
		panel = new ImagePanel("../src/GUI/Background.gif");
		panel.setLayout(new GridLayout(8, 8));
	}
	
	public void createMenu() {
		// Creates a menubar for a JFrame
		JMenuBar menuBar = new JMenuBar();
		
		// Add the menubar to the frame
		setJMenuBar(menuBar);
		
		// Initializes and adds the menu groups
		JMenu fileMenu = new JMenu("Game");
		JMenu helpMenu = new JMenu("Help");
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);
		
		// Initializes the menu items
		JMenuItem newAction = new JMenuItem("New Game");
		JMenuItem undoAction = new JMenuItem("Undo");
		JMenuItem exitAction = new JMenuItem("Exit");
		JMenuItem aboutAction = new JMenuItem("About");
		
		// Sets the icons
		newAction.setIcon(new ImageIcon("../src/GUI/New Game.png"));
		undoAction.setIcon(new ImageIcon("../src/GUI/Undo.png"));
		exitAction.setIcon(new ImageIcon("../src/GUI/Exit.png"));
		aboutAction.setIcon(new ImageIcon("../src/GUI/About.png"));	
		
		newAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				game.initializeGame();
				paintSquares();
			}
		});
		
		undoAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				game.undo();
				game.changePlayer();
				paintSquares();
			}
		});
		
		exitAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		aboutAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Chess Fo Sho! \n By Jeff Chan, James Kuo, Ricky (In)Saini, Bryant Vu", "About!", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		
		// Activate the icons
		fileMenu.add(newAction);
		fileMenu.add(undoAction);
		fileMenu.add(exitAction);
		helpMenu.add(aboutAction);
	}
	
	public void paintSquares() {
		boolean alt = false;
		
		// Create 64 Squares
		for(char i='8'; i>='1'; i--) {
			for(char row : new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}) {
				JButton square = new JButton("" + row +i);
				Square sq = new Square(row, i);
				
				// Transparency setting
				square.setContentAreaFilled(false);
				
				// Set the white border around the JButton
				Border thickBorder = new LineBorder(Color.WHITE, 2);
				square.setBorder(thickBorder);
				
				if(game.getBoard().hasPiece(sq)) { 
					// Get the image for the Piece
					Piece p = game.getBoard().getPiece(sq);
					ImageIcon ico = new ImageIcon("../src/Pieces/"+p.getImage());
					square.setIcon(ico);
				} else {
					// Get the image for an empty JButton Square
					String bg = (alt) ? "Brown" : "Black"; // Either Brown or Black
					ImageIcon ico = new ImageIcon("../src/GUI/"+ bg +".gif");
					square.setIcon(ico);
				}
				
				square.addActionListener(this);
				panel.add(square);
				
				alt = !alt;  // Switch background color.
			}
			alt = !alt; // Switch background color.
		}
		
		// If CheckmateMove, issues a notification.
		if(game.getLastMove() != null && game.getLastMove().getMoveType().equals(MoveType.CHECKMATE))
			JOptionPane.showMessageDialog(null, game.getCurrentPlayerString()+" lost!", "Checkmate!", JOptionPane.INFORMATION_MESSAGE);
	}
	
	/**
	 *	Handles when a JButton Square is clicked.
	 */
	public void actionPerformed(ActionEvent e) {
		String fileAndRank = e.getActionCommand();
		if(lastSelection.equals("") && game.getBoard().hasPiece(new Square(fileAndRank))
			&& game.getBoard().getPiece(new Square(fileAndRank)).getColor() == game.getCurrentPlayer()) {
			// The user selected a Piece to move.
			lastSelection = fileAndRank;
			
			// Highlight the JButton Square.
			lastSelectionButton = (JButton)e.getSource();
			lastSelectionButton.setBorder(new LineBorder(Color.CYAN, 4));
		} else if(lastSelection.length() == 2) {
			// The user selected a target for the selected Piece. Make the move.
			Move move = game.move(new Square(lastSelection), new Square(fileAndRank));
			System.out.println(lastSelection + "-->" + fileAndRank + " = " + move.getClass().getSimpleName());
			
			// Reset and refresh
			lastSelection = "";
            lastSelectionButton.setBorder(new LineBorder(Color.WHITE, 2));
			updateBoard();
		}
		
	}
	
	/**
	 *	Refreshes the GUI.
	 */
	public void updateBoard() {
		panel.removeAll();
		paintSquares();
	}
}